package cn.fl.communication.service.impl;

import cmm.communication.facade.communication.api.INopCallQueryApiService;
import cmm.communication.facade.communication.dto.NopCallBillInfoDTO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.communication.service.INopCallBillInfoService;
import cn.fl.communication.transorm.NopCallBillInfoVOTransform;
import cn.fl.communication.vo.NopCallBillInfoVO;
import cn.fl.system.service.ISysOrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.exception.BaseException;
import cmm.mid.core.framework.page.Pager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 号码保护(话单回调通知)记录 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class NopCallBillInfoServiceImpl implements INopCallBillInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(NopCallBillInfoServiceImpl.class);
	
	/**
	 * 自定义 API 自行注入需要的API服务
	 */
	@Autowired
	private INopCallQueryApiService nopCallQueryApiService;

	@Autowired
	private ISysOrgService sysOrgService;

	@Override
	public Pager searchNopCallBillInfoPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager=nopCallQueryApiService.searchNopCallBillInfoPage(page);
		if(pager == null){
			pager=new Pager();
		}
        List<NopCallBillInfoDTO> dtoList = (List<NopCallBillInfoDTO>) pager.getResultList();
        if(CollectionUtils.isNotEmpty(dtoList)){
			Map<String,String> orgMap=this.sysOrgService.searchBusinessOrgMap();
            List<NopCallBillInfoVO> voList= NopCallBillInfoVOTransform.toVOList(dtoList);
			for (NopCallBillInfoVO vo:voList) {
				if(StringUtils.isNotEmpty(vo.getBuOrgCd())) {
					if(vo.getBuOrgCd().length()>8) {
						vo.setBuOrgCdNm(orgMap.get(vo.getBuOrgCd().substring(0, 8)));
					}
				}
				vo.setbId("狮桥助手");
				vo.setCallChannel("荣联");
				vo.setCallResultNm(getResultBillInfo(vo.getCallResult()));
			}
            pager.setResultList(voList);
        }else {
			pager.setResultList(new ArrayList<NopCallBillInfoVO>());
		}
        return pager;
	}

	private String getResultBillInfo(String code){
		Map<String,String> map = new HashMap<>();
		map.put("0","成功");
		map.put("1","忙");
		map.put("2","无应答");
		map.put("3","客户提前挂机");
		map.put("9","呼叫失败");
		map.put("11","主叫号码与中间号没有关联");
		map.put("14","黑名单");
		String s = map.get(code);
		if(StringUtils.isBlank(s)){
			return "状态:"+code+":未知";
		}else{
			return s;
		}

	}
}
